//
//  DBManager.m
//  LoaginAndRegist
//
//  Created by lanou3g on 16/4/26.
//  Copyright © 2016年 liman. All rights reserved.
//

#import "DBManager.h"
#import "FMDatabase.h"
#import "UserInfoManager.h"
#import "LMHomeListModel+HTML.h"

@implementation DBManager

+(instancetype)shareManager{
    static DBManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[DBManager alloc]init];
    });
    return manager;
}

//数据库指针
static FMDatabase *database = nil;

//打开数据库

-(void)openDB{
    NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
//    数据库路径
    NSString *dbPath = [documentPath stringByAppendingPathComponent:@"PK.sqlite"];
//    NSLog(@"%@****************数据库路径",dbPath);
//    打开创建数据库
    database = [FMDatabase databaseWithPath:dbPath];
    if ([database open]) {
        NSLog(@"数据库打开成功");
    }
}

/**
 *创建表
 */
- (void)creatTable{
//    注意顺序
    NSString *sql = @"create table if not exists shoucang (collectid  integer primary key  autoincrement , uid text , contentid text , title text , content text ,coverimg text , uname text , html text )";
    BOOL result =  [database executeUpdate:sql];
    if (result) {
        NSLog(@"创建表成功");
    }
}

//插入数据

- (void)insertModel:(LMHomeListModel *)model html:(NSString *)html{

//    BOOL result = [database executeUpdateWithFormat:@"insert into shoucang (uid, contentid , title , content , coverimg , uname , html ) values (%@ %@ %@ %@ %@ %@ %@)",[UserInfoManager shareManager].uid,model.ID,model.title,model.content,model.coverimg,model.name,html];
    BOOL result = [database executeUpdateWithFormat:@"INSERT INTO shoucang (uid, contentid,title,content,coverimg,uname,html) VALUES(%@,%@,%@,%@,%@,%@,%@)",[UserInfoManager shareManager].uid,model.ID,model.title,model.content,model.coverimg,model.name,html];
    if (result) {
        NSLog(@"插入数据成功");
    }
}



///收藏方法
- (void)collectDetailModel:(LMHomeListModel *)dModel html:(NSString *)htmlStr{
    [self openDB];
    [self creatTable];
    [self insertModel:dModel html:htmlStr];
    
}
///移除数据
-(void)cancelCollectArticle:(NSString *)connentID{
    [self openDB];
    [self creatTable];
    
    BOOL result = [database executeUpdateWithFormat:@"delete from shoucang where contentid = %@",connentID];
    if (result) {
        NSLog(@"移除成功");
    }
}

- (NSArray *)selectAllCollect{
    [self openDB];
    [self creatTable];
    
    FMResultSet *resultSet = [database executeQueryWithFormat: @"select * from shoucang where uid = %@",[UserInfoManager shareManager].uid];
    NSMutableArray *mArray = [NSMutableArray array];
    
    while ([resultSet next]) {
        LMHomeListModel *model = [[LMHomeListModel alloc]init];
//        赋值
        model.content = [resultSet stringForColumn:@"content"];
        model.coverimg = [resultSet stringForColumn:@"coverimg"];
        model.ID = [resultSet stringForColumn:@"contentid"];
        model.title = [resultSet stringForColumn:@"title"];
        model.name = [resultSet stringForColumn:@"uname"];
        model.htmlStr = [resultSet stringForColumn:@"html"];
        [mArray addObject:model];
    }
    return mArray;
}



- (BOOL) hasCollect:(NSString *)contentID{
    [self openDB];
    [self creatTable];
    BOOL hanCollect = NO;
    FMResultSet *result   = [database executeQueryWithFormat:@"select * from shoucang where uid = %@ and contentid = %@",[UserInfoManager shareManager].uid,contentID ];
    while ([result next]) {
        hanCollect = YES;
    }
    return hanCollect;
}




@end
